

package com.zdkj.subway.stm.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.entity.SysDictItem;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.stm.constant.StmConstans;
import com.zdkj.subway.stm.dto.in.SwTvmSettleCycleCalculateDTO;
import com.zdkj.subway.stm.dto.in.SwTvmSettleSubmitDTO;
import com.zdkj.subway.stm.entity.SwStTvmDailyBill;
import com.zdkj.subway.stm.entity.SwStTvmSettle;
import com.zdkj.subway.stm.service.SwStTvmSettleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * tvm结算
 *
 * @author songtuo
 * @date 2020-08-09 17:14:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swsttvmsettle")
@Api(value = "swsttvmsettle", tags = "tvm结算管理")
@Slf4j
public class SwStTvmSettleController {

    private final SwStTvmSettleService swStTvmSettleService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param swStTvmSettle tvm结算
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwStTvmSettlePage(Page page, SwStTvmSettle swStTvmSettle) {
        return R.ok(swStTvmSettleService.page(page, Wrappers.query(swStTvmSettle)));
    }


    /**
     * 通过id查询tvm结算
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swStTvmSettleService.getById(id));
    }

    /**
     * 新增tvm结算
     *
     * @return R
     */
    @ApiOperation(value = "tvm长周期结算", notes = "tvm长周期结算")
    @SysLog("tvm长周期结算")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('swsttvmsettle_long_cycle_sellte')")
    public R longCycleSellte(@RequestBody SwTvmSettleCycleCalculateDTO settleCycleCalculateDTO) {
        return R.ok(swStTvmSettleService.longCycleSellte(settleCycleCalculateDTO));
    }

    /**
     * tvm收入结算数据 批量提交
     *
     * @param submitDTO
     * @return
     */
    @ApiOperation(value = "tvm收入结算数据 批量提交", notes = "tvm收入结算数据 批量提交")
    @PostMapping("/approval/status")
    @PreAuthorize("@pms.hasPermission('swsttvmsettle_approval_status')")
    public R submit(@RequestBody SwTvmSettleSubmitDTO submitDTO) {
        return R.ok(swStTvmSettleService.submit(submitDTO));
    }

    /**
     * 通过id查询tvm收入结算详情
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询tvm收入结算详情", notes = "通过id查询tvm收入结算详情")
    @GetMapping("/queryDetails/{id}")
    public R querySwTvmSettleDetails(@PathVariable("id") Integer id) {
        return R.ok(swStTvmSettleService.querySwTvmSettleDetails(id));
    }

    @ApiOperation(value = "导出Tvm结算数据", notes = "导出Tvm结算数据")
    @GetMapping("/export")
    public void export(Page page, SwStTvmSettle SwStTvmSettle, HttpServletResponse response) {
        List<SwStTvmSettle> records = swStTvmSettleService.page(page, Wrappers.query(SwStTvmSettle)).getRecords();
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("Tvm结算数据", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");
            EasyExcel.write(response.getOutputStream(), SwStTvmSettle.class).
                    sheet("Tvm结算数据").doWrite(records);
        } catch (IOException e) {
            log.error("导出Tvm结算数据");
        }
    }


//    /**
//     * 修改tvm结算
//     * @param swStTvmSettle tvm结算
//     * @return R
//     */
//    @ApiOperation(value = "修改tvm结算", notes = "修改tvm结算")
//    @SysLog("修改tvm结算" )
//    @PutMapping
//    @PreAuthorize("@pms.hasPermission('generator_swsttvmsettle_edit')" )
//    public R updateById(@RequestBody SwStTvmSettle swStTvmSettle) {
//        return R.ok(swStTvmSettleService.updateById(swStTvmSettle));
//    }

//    /**
//     * 通过id删除tvm结算
//     * @param id id
//     * @return R
//     */
//    @ApiOperation(value = "通过id删除tvm结算", notes = "通过id删除tvm结算")
//    @SysLog("通过id删除tvm结算" )
//    @DeleteMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('generator_swsttvmsettle_del')" )
//    public R removeById(@PathVariable Integer id) {
//        return R.ok(swStTvmSettleService.removeById(id));
//    }

}
